#===============================================================================
# 6_text_study_figure_9_appF.R
# Purpose: To replicate Appendix F, Figure 9: 
#          "Modeling IRR in the text study I"
#===============================================================================

# PACKAGES
#===============================================================================
library(dplyr)
library(tidyr)
library(ggplot2)
library(broom)
library(lme4)


# DATA
#===============================================================================
# - load the pairwise dataset. N = 4,417
db <- read.csv("./data/text-experiment-merged-dataset-PAIRWISE-IRR.csv")

# - define references categories for the categorical variables
db <- db %>% 
  mutate(training = factor(training, levels = c("Basic", "Intermediate", 
                                                "Advanced")),
         task = relevel(factor(task), ref = "DIRECTED_AT")) %>%
  rename(sameideology = samgeideology) %>%
  na.omit() # dropping 2 obs. with NAs. N = 4,415

# MAIN
#===============================================================================

# - estimate models shown in Appendix F, Figure 9

# Model 1: samegender + sameideology + task + training
model01 <- lmer(irr_cohen ~ samegender + sameideology + task + training + 
                  (1 | pair), data = db)

# Model 2: samegender + sameideology + task + training [NO FEELINGS]
model02 <- lmer(irr_cohen ~ samegender + sameideology + task + training + 
                    (1 | pair), data = db %>% 
                    filter(!(task %in% c("ANGRY_FEEL", "ENTHU_FEEL"))))

# - organize the model output, and prepare dataset for plotting

# ... combine all the model results
allmodels <- rbind(
  # Model 5
  as.data.frame(summary(model01)$coefficients) %>% 
    mutate(term = rownames(summary(model01)$coefficients)) %>%
    rename(estimate = Estimate, std.error = `Std. Error`, statistic = `t value`) %>%
    mutate(model = "Model 1 (incl. feeling tasks)"),
  # Model 6
  as.data.frame(summary(model02)$coefficients) %>% 
    mutate(term = rownames(summary(model02)$coefficients)) %>%
    rename(estimate = Estimate, std.error = `Std. Error`, statistic = `t value`) %>%
    mutate(model = "Model 2 (excl. feeling tasks)")
)

# ... provide human-readable variable name; and calculate 95% conf. intervals
plotdb <- allmodels %>%
  filter(term != "(Intercept)") %>%
  mutate(term = recode(term, 
                       `samegender` = "Same gender",
                       `sameideology` = "Same ideology",
                       `taskANGRY_FEEL` = "[Task] Feeling angry\n(ref: Directed at)",
                       `taskENTHU_FEEL` = "[Task] Feeling enthusiastic\n(ref: Directed at)",
                       `taskCONS_VIEW` = "[Task] Conservative view\n(ref: Directed at)",
                       `taskPROGR_VIEW` = "[Task] Progressive view\n(ref: Directed at)",
                       `taskNEG_TONE` = "[Task] Negative tone\n(ref: Directed at)",
                       `taskGENDER` = "[Task] Gender issue\n(ref: Directed at)",
                       `trainingIntermediate` = "[Training] Intermediate\n(ref: Basic)",
                       `trainingAdvanced` = "[Training] Advanced\n(ref: Basic)",
                       `samegender:trainingIntermediate` = "(Same gender) X (Training)\nIntermediate (Ref:Basic)",
                       `samegender:trainingAdvanced` = "(Same gender) X (Training)\nAdvanced (Ref:Basic)",
                       `samegender:taskANGRY_FEEL` = "(Same gender) X (Task)\nFeeling angry (Ref: Directed at)",
                       `samegender:taskCONS_VIEW` = "(Same gender) X (Task)\nConservative view (Ref: Directed at)",
                       `samegender:taskENTHU_FEEL` = "(Same gender) X (Task)\nFeeling enthusiastic (Ref: Directed at)",
                       `samegender:taskGENDER` = "(Same gender) X (Task)\nGender issue (Ref: Directed at)",
                       `samegender:taskNEG_TONE` = "(Same gender) X (Task)\nNegative tone (Ref: Directed at)",
                       `samegender:taskPROGR_VIEW` = "(Same gender) X (Task)\nProgressive view (Ref: Directed at)",
                       `sameideology:trainingIntermediate` = "(Same ideology) X (Training)\nIntermediate (Ref: Basic)",
                       `sameideology:trainingAdvanced` = "(Same ideology) X (Training)\nAdvanced (Ref: Basic)",
                       `sameideology:taskANGRY_FEEL` = "(Same ideology) X (Task)\nFeeling angry (Ref: Directed at)",
                       `sameideology:taskCONS_VIEW` = "(Same ideology) X (Task)\nConservative view (Ref: Directed at)",
                       `sameideology:taskENTHU_FEEL` = "(Same ideology) X (Task)\nFeeling enthusiastic (Ref: Directed at)",
                       `sameideology:taskGENDER` = "(Same ideology) X (Task)\nGender issue (Ref: Directed at)",
                       `sameideology:taskNEG_TONE` = "(Same ideology) X (Task)\nNegative tone (Ref: Directed at)",
                       `sameideology:taskPROGR_VIEW` = "(Same ideology) X (Task)\nProgressive view (Ref: Directed at)"),
         term = factor(term, levels = rev(c(
           "Same gender",
           "Same ideology",
           "[Task] Gender issue\n(ref: Directed at)",
           "[Task] Negative tone\n(ref: Directed at)",
           "[Task] Progressive view\n(ref: Directed at)",
           "[Task] Conservative view\n(ref: Directed at)",
           "[Task] Feeling angry\n(ref: Directed at)",
           "[Task] Feeling enthusiastic\n(ref: Directed at)",
           "[Training] Intermediate\n(ref: Basic)",
           "[Training] Advanced\n(ref: Basic)",
           "(Same gender) X (Training)\nIntermediate (Ref:Basic)",
           "(Same gender) X (Training)\nAdvanced (Ref:Basic)",
           "(Same gender) X (Task)\nGender issue (Ref: Directed at)",
           "(Same gender) X (Task)\nNegative tone (Ref: Directed at)",
           "(Same gender) X (Task)\nProgressive view (Ref: Directed at)",
           "(Same gender) X (Task)\nConservative view (Ref: Directed at)",
           "(Same gender) X (Task)\nFeeling angry (Ref: Directed at)",
           "(Same gender) X (Task)\nFeeling enthusiastic (Ref: Directed at)",
           "(Same ideology) X (Training)\nIntermediate (Ref: Basic)",
           "(Same ideology) X (Training)\nAdvanced (Ref: Basic)",
           "(Same ideology) X (Task)\nGender issue (Ref: Directed at)",
           "(Same ideology) X (Task)\nNegative tone (Ref: Directed at)",
           "(Same ideology) X (Task)\nProgressive view (Ref: Directed at)",
           "(Same ideology) X (Task)\nConservative view (Ref: Directed at)",
           "(Same ideology) X (Task)\nFeeling angry (Ref: Directed at)",
           "(Same ideology) X (Task)\nFeeling enthusiastic (Ref: Directed at)"
         ))),
         lwr = estimate - (1.96 * std.error),
         upr = estimate + (1.96 * std.error)) %>%
  rename(pe = estimate)

# - the plot
ggplot(plotdb,
       aes(x = term, y = pe, ymin = lwr, ymax = upr)) +
  geom_segment(aes(x = term, xend = term, y = lwr, yend = upr), linewidth = 7,
               color = "gray60") +
  geom_point(aes(x = term, y = pe), size = 1, color = "black") +
  geom_hline(yintercept = 0, color = "red") +
  facet_wrap(~ model, nrow = 1) +
  scale_y_continuous("\nCoefficient estimates") +
  scale_x_discrete("") +
  coord_flip() +
  theme(panel.background = element_blank(),
        panel.grid.major = element_line(linetype = "dotted", color = "gray80"),
        strip.text = element_text(size = 12),
        axis.text = element_text(size = 10))

ggsave("./results/appendix_f/appF_9_linear_IRR_preds.pdf",
       width = 25, height = 15, units = "cm", dpi = 300)
